package com.xmut.bs.handaccount.util;

import android.content.Context;
import android.support.design.widget.BottomSheetDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;

import com.xmut.bs.handaccount.R;

import java.util.Calendar;

/**
 * Author Dollkey
 * Date 2017/10/12 16:54.
 */

public class TimeSelectUtil {

    public static final int TYPE_DATE = 0x01;
    public static final int TYPE_TIME = 0x02;
    public static final int TYPE_DATETIME = 0x03;


    public interface DateSelectListener {
        void onDateSelectListener(DatePicker datePicker, TimePicker timePicker);
    }

    public static BottomSheetDialog createDialog(Context context, int type, final DateSelectListener confirmOnClickListener) {
        return createDialog(context, type, true, confirmOnClickListener);
    }

    public static String getDateTime(DatePicker datePicker, TimePicker timePicker) {
        return datePicker.getYear() + "-" + (datePicker.getMonth() + 1) + "-" +
                datePicker.getDayOfMonth() + " " + timePicker.getCurrentHour() + ":" +
                timePicker.getCurrentMinute();
    }

    public static String getTime(TimePicker timePicker) {
        return timePicker.getCurrentHour() + ":" + timePicker.getCurrentMinute();
    }

    public static String getDate(DatePicker datePicker) {
        return datePicker.getYear() + "-" + (datePicker.getMonth() + 1) + "-" +
                datePicker.getDayOfMonth();
    }

    public static BottomSheetDialog createDialog(Context context, int type, boolean hasMinDate, final DateSelectListener confirmOnClickListener) {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        final BottomSheetDialog dialog = new BottomSheetDialog(context);
        View dialogView = LayoutInflater.from(context).inflate(R.layout.pop_time_select, null);
        final DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
        final TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);
        dialogView.findViewById(R.id.cancel_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialogView.findViewById(R.id.confirm_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                confirmOnClickListener.onDateSelectListener(datePicker, timePicker);
            }
        });
        if (type == TYPE_DATE) {
            timePicker.setVisibility(View.GONE);
        } else if (type == TYPE_TIME) {
            datePicker.setVisibility(View.GONE);
        }
        if (hasMinDate)
            datePicker.setMinDate(System.currentTimeMillis());
        datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            }
        });
        timePicker.setIs24HourView(true);
        timePicker.setCurrentHour(hour);
        timePicker.setCurrentMinute(minute);
        dialog.setContentView(dialogView);
        return dialog;
    }
}
